<template>
	<view>
		<BrandClassification @getBrandName="getBrandName" :isClass="true" :items="brandArr" :indexList="indexList" :hot="categoryData[0].children" v-if="categoryData.length > 0"></BrandClassification> 
	</view>
</template>

<script>
	import BrandClassification from "../../components/BrandClassification/BrandClassification"
	import {
		getCategory
	} from "../../common/api.js"
	
	export default {
		data() {
			return {
				categoryData: [],
				brandArr: [],
				indexList: [],
				goodsName: "",
				commodityEn:"",
				fatherUrl: null,
				goods: null,
				wish: null
			}
		},
		components: {
			BrandClassification
		},
		methods: {
			getBrandName(cell) {
				console.log(cell); 
				const item = encodeURIComponent(JSON.stringify(cell));
				const tempGoodsName = encodeURIComponent(JSON.stringify(this.goodsName));
				const commodityEnName = encodeURIComponent(JSON.stringify(this.commodityEn));
			
				console.log(this.wish);
				if(this.goods) {
					uni.redirectTo({
						url: `/pages/${this.fatherUrl}/${this.fatherUrl}?wish=${this.wish}&goods=${encodeURIComponent(JSON.stringify(this.goods))}&item=${item}`
					});
				} else {
					uni.redirectTo({
						url: `/pages/${this.fatherUrl}/${this.fatherUrl}?goodsName=${tempGoodsName}&wish=${this.wish}&item=${item}&commodityEn=${commodityEnName}`
					});
				}
			},
			initData() {
			
				getCategory()
					.then((res) => {
						console.log(res,"111");
						res[0].children = res[0].children.filter(
						(item) => {
						return item.id != "308";
						}
						);
						this.categoryData = res;
					
						this.modifData(res[0].children);
					})
			},
			modifData(arr) {
				// 修改数据品牌右侧显示内容 
				const newArr = arr.map((item) => {
					item["newBrandName"] = item.brandEngName;
					return item;
				})
				const tempArr = [];
				newArr.sort(function(s1, s2) {
					let x1 = s1.newBrandName.toUpperCase();
					let x2 = s2.newBrandName.toUpperCase();
					if (x1 < x2) {
						return -1;
					}
					if (x1 > x2) {
						return 1;
					}
					return 0;
				});
				// console.log(newArr);
				newArr.forEach((subItem, index) => {
					let isAdd = false;
					tempArr.forEach((item, index) => {
						if (item[0].newBrandName[0].toUpperCase() === subItem.newBrandName[0]
							.toUpperCase()) {
							isAdd = true;
							tempArr[index].push(subItem);
						}
					})
					if (!isAdd && tempArr.length > 0) {
						tempArr.push([subItem])
					}
					if (tempArr.length <= 0) {
						tempArr.push([subItem])
					}
				})
				this.brandArr = tempArr;
				tempArr.forEach((item) => {
					this.indexList.push(item[0].newBrandName[0].toUpperCase());
				})
			}
		},
		created() {
			this.initData();
		},
		onLoad(option) {
			console.log(option);
			if(option.url) this.fatherUrl = option.url;
			if(option.goodsName) this.goodsName = option.goodsName;
			
			if(option.commodityEn) this.commodityEn = option.commodityEn;
		
			if(option.goods) this.goods = JSON.parse(decodeURIComponent(option.goods));
			console.log(option.wish);
			if(option.wish) this.wish = option.wish;
		},
		onReady() {
			uni.setNavigationBarTitle({ title: this.$t("brandClassify.title")}); 
		},
		onBackPress(e) {
			console.log(123);
			uni.redirectTo({
				url: `/pages/${this.fatherUrl}/${this.fatherUrl}`
			})
			return true;
		},
	}
</script>

<style>
	page {
		background-color: #FBFBFB;
	}
</style>
<style lang="scss" scoped>

</style>